<template>
  <dialog-edit :visible.sync="dialogVisible" :title="$attrs.title" ref="userManageEdit" width="850" @save="save" :remember="false" :destroyOnClose="true"
               :height="heightClass()" :saveFlag="true" :loading="loading" :escClosable="true" :closeFlag="true" :saveLoading="saveLoading">
    <template v-slot:body>
      <div style="margin-bottom: 10px">
        <vxe-form :data="formData" :span="24" title-align="right" :rules="formRules"
                  :title-width="100" ref="formRefs">
          <div class="allq">
            <vxe-form-item title="分类" field="Type" v-if="taskEditDialogType === 'edit' || taskEditDialogType === 'edit1'">
              <template v-slot>
                <!--            <vxe-input v-model="formData.name" placeholder="请选择分类" clearable></vxe-input>-->
                <vxe-select v-model="formData.Type" placeholder="请选择分类">
                  <vxe-option v-for="type in typeLists" :key="type.value" :value="type.value" :label="`${type.title}`"></vxe-option>
                </vxe-select>
              </template>
            </vxe-form-item>
            <vxe-form-item title="指派给" field="Acceptid" v-if="taskEditDialogType === 'edit' || taskEditDialogType === 'edit1'">
              <select-project-team v-model="formData.Acceptid" :clearable="true" :url="userApi" getPage="getPage" :queryValue="{}"
                                   responseData="Users" searchKeyword="keywords" :showValueShow="showValueShow"
                                   column="Realname" clickValue="Userid" showValue="Realname"></select-project-team>
<!--              <select-project-team v-model="formData.Acceptid" :clearable="true" :showValueShow="showValueShow"></select-project-team>-->
            </vxe-form-item>
            <vxe-form-item title="版本号" field="Vesionno" v-if="isShowVersion">
              <vxe-textarea v-model="formData.Vesionno" placeholder="请输入版本号或者分支" :rows="8"></vxe-textarea>
            </vxe-form-item>
            <vxe-form-item title="验收环境" field="Acceptevn" v-if="isShowVersion">
              <select-diction v-model="formData.Acceptevn" DictTypeCode="TEST_YSHJ" :placeholder = "'验收环境'"></select-diction>
            </vxe-form-item>
            <vxe-form-item title="直接原因" field="DirectCause" v-if="taskEditDialogType === 'edit' || isTypeTemp">
              <vxe-textarea v-model="formData.DirectCause" placeholder="请输入直接原因" :rows="8"></vxe-textarea>
            </vxe-form-item>
            <vxe-form-item title="根本原因" field="RootCause" v-if="taskEditDialogType === 'edit' || isTypeTemp">
              <vxe-textarea v-model="formData.RootCause" placeholder="请输入根本原因" :rows="8"></vxe-textarea>
            </vxe-form-item>
            <vxe-form-item title="根本原因分类" field="RootCauseType" v-if="taskEditDialogType === 'edit' || isTypeTemp">
              <select-diction v-model="formData.RootCauseType" DictTypeCode="ROOT_CAUSE_TYPE" :placeholder = "'根本原因分类'"></select-diction>
            </vxe-form-item>
            <vxe-form-item title="对应策略" field="CorrespondingStrategy" v-if="taskEditDialogType === 'edit' || isShowCL">
<!--              <vxe-textarea v-model="formData.CorrespondingStrategy" rows="8"></vxe-textarea>-->
              <wangeditor v-model="formData.CorrespondingStrategy" :isClear="isClear"
                          @change="$refs.formRefs.updateStatus(scope)" class="wange1"></wangeditor>
            </vxe-form-item>
            <vxe-form-item title="规避策略" field="EvasiveStrategy" v-if="taskEditDialogType === 'edit'">
              <vxe-textarea v-model="formData.EvasiveStrategy" :rows="8"></vxe-textarea>
            </vxe-form-item>
            <vxe-form-item title="预计工时" field="EvasiveStrategy" v-if="taskEditDialogType === 'edit' || taskEditDialogType === 'edit1'">
              <vxe-input v-model="formData.Estimatedtasktime" placeholder="数值范围限制 0-500" type="number" min="0" max="500" clearable></vxe-input>
            </vxe-form-item>
            <vxe-form-item title="实际工时" field="EvasiveStrategy" v-if="taskEditDialogType === 'edit' || taskEditDialogType === 'edit1'">
              <vxe-input v-model="formData.Actualtasktime" placeholder="数值范围限制 0-500" type="number" min="0" max="500" clearable></vxe-input>
            </vxe-form-item>
          </div>
          <vxe-form-item title="备注评论" field="Note">
            <template v-slot="scope">
              <wangeditor v-model="formData.Note" :isClear="isClear"
                          @change="$refs.formRefs.updateStatus(scope)" class="wange"></wangeditor>
            </template>
          </vxe-form-item>
          <vxe-form-item title="附件" field="Attachment" v-if="taskEditDialogType === 'add1'">
<!--            <template v-slot="scope">-->
            <btn-upload label="附件" v-model="formData.Attachment" uploadApi="/v1/upload/" type="file" @uploadSuccess="uploadSuccess" :showFileName="showFileName"></btn-upload>
<!--            </template>-->
          </vxe-form-item>
        </vxe-form>
      </div>
    </template>
  </dialog-edit>
</template>

<script>
  import dialogEdit from '@/components/element/dialogEdit'
  import SelectDiction from '@/components/element/SelectDiction'
  import SelectProjectTeam from "@/components/element/select/SelectProjectTeam";
  import projectTestApi from "@/api/projectTestApi";
  import wangeditor from '@/components/element/wangeditor/index';
  import userApi from '@/api/userApi'
  import BtnUpload from "@/components/element/btnUpload";
  export default {
    name: "TaskEditDialog",
    components: {
      BtnUpload,
      SelectProjectTeam,
      SelectDiction,
      dialogEdit,
      wangeditor
    },
    props: {
      clickModel: {
        type: String,
        default() {
          return '';
        }
      },
      isShowCL: {
        type: Boolean,
        default() {
          return false;
        }
      },
      isShowVersion: {
        type: Boolean,
        default() {
          return false;
        }
      },
      taskEditDialogType: {
        type: String,
        default() {
          return 'edit';
        }
      },
      visible: {
        type: Boolean,
        default() {
          return false;
        }
      },
      preFormData: {
        type: Object,
        default() {
          return {};
        }
      }
    },
    data () {
      return {
        isTypeTemp: false,
        showFileName: false,
        userApi: userApi,
        formData: {},
        showValueShow: {},
        saveLoading: false,
        isClear: true,
        formRules: {
          Type: [
            { required: true, message: '分类必填', trigger: 'change' },
            // { validator: this.validator().AcceptidValidator, trigger: 'blur' }
          ],
          Acceptid: [
            { required: true, message: '指派人必填', trigger: 'change' },
          ],
          DirectCause: [
            { required: true, message: '直接原因必填', trigger: 'change' },
          ],
          Vesionno: [
            // { required: true, message: '版本号必填', trigger: 'change' },
          ],
          Acceptevn: [
            { required: true, message: '验收环境必填', trigger: 'change' },
          ],
        },
        loading: false,
        typeLists: [
          {
            value: '1',
            title: '程序bug'
          },
          {
            value: '2',
            title: '需求问题'
          },
          {
            value: '3',
            title: '需求变更'
          },
          {
            value: '4',
            title: '无需对应'
          },
          {
            value: '5',
            title: '设计问题'
          },
          {
            value: '6',
            title: '数据问题'
          },
          {
            value: '7',
            title: '版本问题'
          },
          {
            value: '8',
            title: '环境问题'
          },
          {
            value: '9',
            title: '重复'
          },
          {
            value: '10',
            title: '用户体验'
          }
        ]
      }
    },
    computed: {
      dialogVisible: {
        get: function () {
          return this.visible
        },
        set: function (value) {
          this.$emit('update:visible', value)
        }
      },
    },
    methods: {
      heightClass () {
        if (this.taskEditDialogType === 'edit') {
          return '80%'
        } else if (this.taskEditDialogType === 'add') {
          return '450'
        } else if (this.taskEditDialogType === 'add1') {
          return '480'
        }
        return '80%'
      },
      uploadSuccess (returnData, rowData, fileList) {
        this.showFileName = true
        this.formData.Attachmentname = returnData.fileName
      },
      save () {
        this.$refs.formRefs.validate().then(_ => {
          this.allFunction().submit()
        }).catch(_ => {
        })
      },
      allFunction () {
        return {
          // 调用返回接口
          submit: () => {
            this.saveLoading = true
            if (this.taskEditDialogType === 'add1') {
              this.formData.Status = '-1'
            }
            projectTestApi.status(this.formData).then(res => {
              this.$XModal.message({message: res.message, status: res.code === 1 ? 'success' : 'error'})
              if (res.code === 1) {
                delete this.formData.Attachment
                delete this.formData.Attachmentname
                this.$emit('saveClose', this.formData)
                this.$nextTick(_ => {
                  this.dialogVisible = false
                })
              }
            }).finally(_ => {
              this.saveLoading = false
            })
          }
        }
      },
    },
    watch: {
      dialogVisible: {
        handler (n, o) {
          this.showFileName = false
          if (n) {
            if (this.$refs.formRefs) {
              this.$refs.formRefs.clearValidate()
            }
            this.$nextTick(_ => {
              this.formData = JSON.parse(JSON.stringify(this.preFormData))
              this.formData.Attachment = ''
              if (!this.formData.Acceptevn || this.formData.Acceptevn === '0') {
                this.formData.Acceptevn = ''
              }
              // if ([20, 21, 22].some(it => it === Number(this.clickModel || '0'))) {
              //   this.formRules.Acceptevn[0].required = false
              // } else {
              //   this.formRules.Acceptevn[0].required = true
              // }
              this.formData.Attachmentname = ''
              if (this.formData.hasOwnProperty('Note')) {
                this.formData.Note = ''
              } else {
                this.$set(this.formData, 'Note', '')
              }
              if (this.formData.hasOwnProperty('Type')) {
                this.$set(this.formData, 'Type', this.formData.Type.toString())
                this.formData.Type = this.formData.Type === '0' ? '' : this.formData.Type
              } else {
                this.$set(this.formData, 'Type', '1')
              }
              if (this.formData.hasOwnProperty('RootCauseType')) {
                this.$set(this.formData, 'RootCauseType', (this.formData.RootCauseType).toString())
                this.formData.RootCauseType = this.formData.RootCauseType === '0' ? '' : this.formData.RootCauseType
              } else {
                this.$set(this.formData, 'RootCauseType', '1')
              }
              if (this.clickModel === '20' || this.clickModel === '21') {
                this.isTypeTemp = true
              } else {
                this.isTypeTemp = false
              }
              this.formRules.DirectCause[0].required = !(this.clickModel === '20' || this.clickModel === '21')
              this.showValueShow = { 'Realname': this.formData.Acceptrealname }
            })
          } else {
            this.formData = {}
          }
        }
      }
    }
  }
</script>

<style scoped>
  .wange >>> .w-e-text{
    overflow-y: hidden;
  }
  .allq >>> .vxe-form--item {
    width: 100%;
  }
</style>
